package com.geek.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.PurchaseReturnAudit;
import com.geek.factory.result.Result;
import com.geek.factory.service.IPurchaseReturnAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 退货审核记录管理控制器
 */
@RestController
@RequestMapping("/purchase-return-audit")
@Api(tags = "后台系统的 - 退货审核记录模块")
@Slf4j
@CrossOrigin
public class PurchaseReturnAuditController {

    @Autowired
    private IPurchaseReturnAuditService purchaseReturnAuditService;

    @ApiOperation("通过ID查询退货审核记录")
    @GetMapping("/{auditId}")
    public Result findPurchaseReturnAuditById(@ApiParam("审核记录ID") @PathVariable("auditId") Integer auditId) {
        PurchaseReturnAudit audit = purchaseReturnAuditService.getById(auditId);
        log.info("purchaseReturnAudit: {}", audit);
        if (audit != null) {
            return new Result(SuccessConstant.SUCCESS, "ok", audit);
        }
        return new Result(SuccessConstant.FAIL, "退货审核记录不存在");
    }

    @ApiOperation("新增退货审核记录")
    @PostMapping
    public Result addPurchaseReturnAudit(@RequestBody PurchaseReturnAudit audit) {
        audit.setCreatedTime(LocalDateTime.now());
        // audit.setCreatedBy("admin"); // 实际应从登录用户获取
        boolean isSuccess = purchaseReturnAuditService.save(audit);
        log.info("新增退货审核记录: {}, 结果: {}", audit, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "新增成功", audit);
        }
        return new Result(SuccessConstant.FAIL, "新增失败");
    }

    @ApiOperation("根据ID删除退货审核记录")
    @DeleteMapping("/{auditId}")
    public Result deletePurchaseReturnAudit(@ApiParam("审核记录ID") @PathVariable("auditId") Integer auditId) {
        boolean isSuccess = purchaseReturnAuditService.removeById(auditId);
        log.info("删除退货审核记录ID: {}, 结果: {}", auditId, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "删除成功");
        }
        return new Result(SuccessConstant.FAIL, "删除失败");
    }

    @ApiOperation("修改退货审核记录")
    @PutMapping("/{auditId}")
    public Result updatePurchaseReturnAudit(@ApiParam("审核记录ID") @PathVariable("auditId") Integer auditId, @RequestBody PurchaseReturnAudit audit) {
        audit.setAuditId(auditId);
        boolean isSuccess = purchaseReturnAuditService.updateById(audit);
        log.info("修改退货审核记录: {}, 结果: {}", audit, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "修改成功", audit);
        }
        return new Result(SuccessConstant.FAIL, "修改失败");
    }

    @ApiOperation("分页查询退货审核记录列表")
    @GetMapping("/page")
    public Result getPurchaseReturnAuditPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer returnId
    ) {
        QueryWrapper<PurchaseReturnAudit> wrapper = new QueryWrapper<>();
        if (returnId != null) {
            wrapper.eq("return_id", returnId);
        }
        wrapper.orderByDesc("created_time");

        Page<PurchaseReturnAudit> page = purchaseReturnAuditService.page(new Page<>(current, size), wrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("records", page.getRecords());
        data.put("total", page.getTotal());
        data.put("current", page.getCurrent());
        data.put("size", page.getSize());
        data.put("pages", page.getPages());

        return new Result(SuccessConstant.SUCCESS, "查询成功", data);
    }

    @ApiOperation("获取所有退货审核记录")
    @GetMapping("/list")
    public Result getAllPurchaseReturnAudits() {
        List<PurchaseReturnAudit> audits = purchaseReturnAuditService.list();
        return new Result(SuccessConstant.SUCCESS, "查询成功", audits);
    }
}